<template>
	<view class="Wallpaper_img">
		<view class="img">
			<image :src="imgPaht" mode="widthFix" @click="clickBack"></image>
		</view>
		<view class="down">
			<button type="default" size="mini" @click="shoucang">收藏</button>
			<button type="default" size="mini" @click="downImg">保存</button>
		</view>
	</view>

</template>

<script>
	import {
		base_url
	} from '../../util/api.js'
	export default {
		data() {
			return {
				imgPaht: '',
				queryObj: {
					classify: '',
					imgname: ''
				},

				test: ''
			};
		},

		onLoad(options) {
			this.queryObj.classify = options.classify
			this.queryObj.imgname = options.imgname

			this.getImg()
		},

		methods: {
			async getImg() {
				// 最初的图片前端接受

				const {
					data: res
				} = await this.$myRequest({
					url: '/my/wallpapers/getwallpaper/img',
					data: this.queryObj,
					header: {
						'Content-Type': 'application/json'
					},
					responseType: 'arraybuffer'
				})
				const arrayBuffer = new Uint8Array(res)
				const base64 = "data:image/png;base64," + uni.arrayBufferToBase64(arrayBuffer)
				this.imgPaht = base64 || ''
				
				// console.log(res)
				// this.imgPaht = `${base_url}/my/wallpapers/getwallpaper/img?classify=${this.queryObj.classify}&imgname=min_${this.queryObj.imgname}`

			},



			clickBack() {
				uni.navigateBack()
			},
			downImg() {
				let base64 = this.imgPaht.replace(/^data:image\/\w+;base64,/, "") //去掉data:image/png;base64,
				let filePath = wx.env.USER_DATA_PATH + `/${this.queryObj.imgname}`
				var save = uni.getFileSystemManager()
				save.writeFile({
					filePath: filePath,
					data: base64,
					encoding: 'base64',
					success: (res) => {
						console.log(669699, res)
						uni.saveImageToPhotosAlbum({
							filePath: filePath,
							success: function(res) {
								uni.showToast({
									title: '保存成功',
									icon: "success",
									duration: 1000
								})
							},
							fail: function(err) {
								uni.showToast({
									title: '保存失败',
									icon: "success",
									duration: 1000
								})
								console.log(err)
							}

						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	body {
		background-color: #000;
	}

	image {
		height: 100%;
		width: 100%;
		// vertical-align: middle;
	}

	.down {
		display: flex;
		position: absolute;
		bottom: 0;
		left: 50%;
		transform: translate(-50%);

		button {
			margin: 0 20rpx;
			background-color: rgba(0, 0, 0, .3);
			color: #fff;
		}
	}
</style>
